<?php
			//make copy of original authentication.tpl
			$src1 = _PS_THEME_DIR_.'/authentication.tpl';
			$dest1 = dirname(__FILE__).'/bak/authentication.tpl';
			if ( !copy($src1, $dest1))
				die(Tools::displayError('Error: cannot copy Patch file !<br />-> Source: '.$src1.'<br /><- Dest: '.$dest1));
			//patch the original authentication.tpl
			//Crea un campo para introducir el DNI en la sección de la información personal del cliente
			$old1a = '{$smarty.post.customer_lastname}{/if}" />
				<sup>*</sup>
			</p>';
			$new1a ='{$smarty.post.customer_lastname}{/if}" />
				<sup>*</sup>
			</p>
            <p class="required text">
			<label for="customer_dni">{l s=\'Tax Identification Number\'}</label>
			<input onkeyup="$(\'#dni\').val(this.value);" type="text" class="text" id="customer_dni" name="customer_dni" value="{if isset($smarty.post.customer_dni)}{$smarty.post.customer_dni}{/if}" />
			<sup>*</sup>
            <span class="form_info">{l s=\'Passport,...\'}</span>
			</p>';
			//Crea un campo para introducir el DNI en la sección de la dirección del cliente
			$old1b = '{$smarty.post.lastname}{/if}" />
				<sup>*</sup>
			</p>';
			$new1b = '{$smarty.post.lastname}{/if}" />
				<sup>*</sup>
			</p>
            <p class="required text">
				<label for="dni">{l s=\'Tax Identification Number\'}</label>
				<input type="text" class="text" id="dni" name="dni" value="{if isset($smarty.post.dni)}{$smarty.post.dni}{/if}" />
				<sup>*</sup>
			</p>';
			$openfile1 = file_get_contents($src1);
			$search1 = array($old1a,$old1b);
			$replace1 = array($new1a,$new1b);
			$patch1 = str_replace ($search1, $replace1, $openfile1);
			fclose ($openfile1);
			$openfile1 = file_put_contents($src1, $patch1);


			//make copy of original identity.tpl
			$src4 = _PS_THEME_DIR_.'/identity.tpl';
			$dest4 = dirname(__FILE__).'/bak/identity.tpl';
			if ( !copy($src4, $dest4))
				die(Tools::displayError('Error: cannot copy Patch file !<br />-> Source: '.$src4.'<br /><- Dest: '.$dest4));
			//patch the original identity.tpl
			//Crea un campo para introducir el DNI en la información del cliente
			$old4a='post.lastname}" />
			</p>';
			$new4a='post.lastname}" />
			</p>
            <p class="required text">
				<label for="dni">{l s=\'Tax Identification Number\'}<sup>*</sup></label>
				<input type="text" name="dni" id="dni" value="{$smarty.post.dni}" />
			</p>';
			$openfile4 = file_get_contents($src4);
			$search4 = array($old4a);
			$replace4 = array($new4a);
			$patch4 = str_replace ($search4, $replace4, $openfile4);
			fclose ($openfile4);
			$openfile4 = file_put_contents($src4, $patch4);			

			
			//make copy of original Customer.php
			$src5 = _PS_CLASS_DIR_.'/Customer.php';
			$dest5 = dirname(__FILE__).'/bak/Customer.php';
			if ( !copy($src5, $dest5))
				die(Tools::displayError('Error: cannot copy Patch file !<br />-> Source: '.$src5.'<br /><- Dest: '.$dest5));
			//patch the original Customer.php
			$old5a='$firstname;';
			$new5a='$firstname;
	
	/** @var string dni */
	public 		$dni;';
			$old5b='$fieldsRequired = array(\'lastname\', \'passwd\', \'firstname\', \'email\');
 	protected 	$fieldsSize = array(\'lastname\' => 32, \'passwd\' => 32, \'firstname\' => 32, \'email\' => 128);
 	protected 	$fieldsValidate = array(\'secure_key\' => \'isMd5\', \'lastname\' => \'isName\', \'firstname\' => \'isName\',';
			$new5b='$fieldsRequired = array(\'lastname\', \'passwd\', \'firstname\', \'email\', \'dni\');
 	protected 	$fieldsSize = array(\'lastname\' => 32, \'passwd\' => 32, \'firstname\' => 32, \'email\' => 128, \'dni\' => 16);
 	protected 	$fieldsValidate = array(\'secure_key\' => \'isMd5\', \'lastname\' => \'isName\', \'firstname\' => \'isName\',';
			$old5c='pSQL($this->firstname);';
			$new5c='pSQL($this->firstname);
		$fields[\'dni\'] = pSQL(Tools::strtoupper($this->dni));';
			$old5d='getCustomers()
	{
		return Db::getInstance()->ExecuteS(\'
		SELECT `id_customer`, `email`, `firstname`, `lastname`';
			$new5d='getCustomers()
	{
		return Db::getInstance()->ExecuteS(\'
		SELECT `id_customer`, `email`, `firstname`, `lastname`, `dni`';
			$old5e='\'active\' => \'isBool\'';
			$new5e='\'active\' => \'isBool\', \'dni\' => \'isDni\'';
			$openfile5 = file_get_contents($src5);
			$search5 = array($old5a,$old5b,$old5c,$old5d,$old5e);
			$replace5 = array($new5a,$new5b,$new5c,$new5d,$new5e);
			$patch5 = str_replace ($search5, $replace5, $openfile5);
			fclose ($openfile5);
			$openfile5 = file_put_contents($src5, $patch5);
		
		
			//make copy of original Validate.php
			$src6 = _PS_CLASS_DIR_.'/Validate.php';
			$dest6 = dirname(__FILE__).'/bak/Validate.php';
			if ( !copy($src6, $dest6))
				die(Tools::displayError('Error: cannot copy Patch file !<br />-> Source: '.$src6.'<br /><- Dest: '.$dest6));
			//patch the original Validate.php
			$old6a='isHookName($hook)
	{
		return preg_match(\'/^[a-z0-9_-]+$/ui\', $hook);
	}';
			$new6a='isHookName($hook)
	{
		return preg_match(\'/^[a-z0-9_-]+$/ui\', $hook);
	}

	 /**
	* Check for Dni validity
	*
	* @param string $dni to validate
	* @return boolean Validity is ok or not
	*/
	static public function isDni($dni, $size = 16)
    {   
        return $dni=$dni;
    }';

			$openfile6 = file_get_contents($src6);
			$search6 = array($old6a);
			$replace6 = array($new6a);
			$patch6 = str_replace ($search6, $replace6, $openfile6);
			fclose ($openfile6);
			$openfile6 = file_put_contents($src6, $patch6);
		

			//make copy of original PDF.php
			$src7 = _PS_CLASS_DIR_.'/PDF.php';
			$dest7 = dirname(__FILE__).'/bak/PDF.php';
			if ( !copy($src7, $dest7))
				die(Tools::displayError('Error: cannot copy Patch file !<br />-> Source: '.$src7.'<br /><- Dest: '.$dest7));
			//patch the original PDF.php
			//Muestra el DNI en la información de facturación del cliente
			$old7a='$invoiceState = $invoice_address->id_state ? new State($invoice_address->id_state) : false;';
			$new7a='$invoiceState = $invoice_address->id_state ? new State($invoice_address->id_state) : false;
		$invoice_customer = new Customer(intval($invoice_address->id_customer));';
			$old7b='$invoice_address->lastname), 0, \'L\');';
			$new7b='$invoice_address->lastname), 0, \'L\');
		$pdf->Ln(5);
		$pdf->Cell($width, 10, \' \', 0, \'L\');
        $pdf->Cell($width, 10, self::l(\'TIN:\').\' \'.Tools::iconv(\'utf-8\', self::encoding(), $invoice_customer->dni), 0, \'L\');';
			$openfile7 = file_get_contents($src7);
			$search7 = array($old7a, $old7b);
			$replace7 = array($new7a, $new7b);
			$patch7 = str_replace ($search7, $replace7, $openfile7);
			fclose ($openfile7);
			$openfile7 = file_put_contents($src7, $patch7);
		
		
			//make copy of original authentication.php
			$src8 = _PS_ROOT_DIR_.'/authentication.php';
			$dest8 = dirname(__FILE__).'/bak/authentication.php';
			if ( !copy($src8, $dest8))
				die(Tools::displayError('Error: cannot copy Patch file !<br />-> Source: '.$src8.'<br /><- Dest: '.$dest8));
			//patch the original authentication.php
			$old8a='if (Tools::isSubmit(\'submitAccount\'))
{
	$create_account = 1;
	$smarty->assign(\'email_create\', 1);';
			$new8a='//FUNCION PARA VALIDAR DNI, CIF y NIE
	function valida_nif_cif_nie($cif) {
	//Copyright ©2005-2008 David Vidal Serra. Bajo licencia GNU GPL.
	//Este software viene SIN NINGUN TIPO DE GARANTIA; para saber mas detalles
	//puede consultar la licencia en http://www.gnu.org/licenses/gpl.txt
	//Esto es software libre, y puede ser usado y redistribuirdo de acuerdo
	//con la condicion de que el autor jamas sera responsable de su uso.
	//Valores válidos de return: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, 4 = campo DNI vacío cuando no es obligatorio
	//Valores no válidos de return: 0 = desconocido, -1 = NIF erróneo, -2 = CIF erróneo, -3 = NIE erróneo, -4 = campo DNI vacío cuando es obligatorio, -5 = DNI repetido
	//Comprobación de DNI no obligatorio y campo vacío
		$dnivalue = Configuration::get(\'DNI_NEEDED\');
		if ($dnivalue == 0 && empty($cif) && $cif != \'0\')
			return 4;
	//comprobación de DNI obligatorio y campo vacío
		if ($dnivalue == 1 && empty($cif) && $cif != \'0\')
			return -4;
	//comprobación de DNI ya utilizado
		$ndni = strtoupper($_POST[\'dni\']);
		$sql = \'SELECT *
		FROM `\'._DB_PREFIX_.\'customer`
		WHERE `dni` = "\'.$ndni.\'"
		AND `id_customer` != "\'.intval($_POST[\'id\']).\'"\';
		$result = Db::getInstance()->getRow($sql);
		if (!empty ($result))
			return -5;
	//preparación de los datos
		$cif = strtoupper($cif);
		for ($i = 0; $i < 9; $i ++)
			$num[$i] = substr($cif, $i, 1);
	//si no tiene un formato valido devuelve error
		if (!ereg(\'((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)\', $cif))
			return 0;
	//comprobacion de NIFs estandar
		if (ereg(\'(^[0-9]{8}[A-Z]{1}$)\', $cif))
			if ($num[8] == substr(\'TRWAGMYFPDXBNJZSQVHLCKE\', substr($cif, 0, 8) % 23, 1))
				return 1;
			else
				return -1;
	//algoritmo para comprobacion de codigos tipo CIF
		$suma = $num[2] + $num[4] + $num[6];
		for ($i = 1; $i < 8; $i += 2)
			$suma += substr((2 * $num[$i]),0,1) + substr((2 * $num[$i]),1,1);
		$n = 10 - substr($suma, strlen($suma) - 1, 1);
	//comprobacion de NIFs especiales (se calculan como CIFs)
		if (ereg(\'^[KLM]{1}\', $cif))
			if ($num[8] == chr(64 + $n))
				return 1;
			else
	 			return -1;
	//comprobacion de CIFs
		if (ereg(\'^[ABCDEFGHJNPQRSUVW]{1}\', $cif))
			if ($num[8] == chr(64 + $n) || $num[8] == substr($n, strlen($n) - 1, 1))
				return 2;
			else
				return -2;
	//comprobacion de NIEs
		//T
		if (ereg(\'^[T]{1}\', $cif))
			if ($num[8] == ereg(\'^[T]{1}[A-Z0-9]{8}$\', $cif))
				return 3;
			else
				return -3;
		//XYZ
		if (ereg(\'^[XYZ]{1}\', $cif))
			if ($num[8] == substr(\'TRWAGMYFPDXBNJZSQVHLCKE\', substr(str_replace(array(\'X\',\'Y\',\'Z\'), array(\'0\',\'1\',\'2\'), $cif), 0, 8) % 23, 1))
				return 3;
			else
				return -3;
	//si todavia no se ha verificado devuelve error
		return 0;
	}
	//FIN DE VERIFICACION DNI, CIF, NIE.
	
	if (Tools::isSubmit(\'submitAccount\'))
{
	$create_account = 1;
	$smarty->assign(\'email_create\', 1);
	
	$controlDni = valida_nif_cif_nie($_POST[\'dni\']);';
			$old8b='Tools::displayError(\'invalid birthday\');';
			$new8b='Tools::displayError(\'invalid birthday\');
	//modificación para validar dni,cif y nie
	elseif ($controlDni == -5)
		$errors[] = Tools::displayError(\'NIF, CIF or NIE inserted has been already used. Try another.\');
	elseif ($controlDni == -1)
		$errors[] = Tools::displayError(\'NIF inserted is not valid.\');
	elseif ($controlDni == -2)
		$errors[] = Tools::displayError(\'CIF inserted is not valid.\');
	elseif ($controlDni == -3)
		$errors[] = Tools::displayError(\'NIE inserted is not valid.\');
	elseif ($controlDni == -4)
		$errors[] = Tools::displayError(\'Tax Identification Number is mandatory\');
	elseif ($controlDni == 0)
		$errors[] = Tools::displayError(\'NIF, CIF or NIE inserted is not valid.\');
	//fin de verificación';
				$old8c='$_POST[\'firstname\'] : $_POST[\'customer_firstname\'];
		$_POST[\'lastname\'] = $_POST[\'customer_lastname\'];
		$_POST[\'firstname\'] = $_POST[\'customer_firstname\'];
		$errors = $customer->validateControler();
		$_POST[\'lastname\'] = $addrLastname;
		$_POST[\'firstname\'] = $addrFirstname;'; 
			$new8c='$_POST[\'firstname\'] : $_POST[\'customer_firstname\'];
		$addrDni = isset($_POST[\'dni\']) ? $_POST[\'dni\'] : $_POST[\'customer_dni\'];
		$_POST[\'lastname\'] = $_POST[\'customer_lastname\'];
		$_POST[\'firstname\'] = $_POST[\'customer_firstname\'];
		$_POST[\'dni\'] = $_POST[\'customer_dni\'];
		$errors = $customer->validateControler();
		$_POST[\'lastname\'] = $addrLastname;
		$_POST[\'firstname\'] = $addrFirstname;
		$_POST[\'dni\'] = $addrDni;';
			$old8d='array(\'{firstname}\' => $customer->firstname, \'{lastname}\' => $customer->lastname, \'{email}\' => $customer->email, \'{passwd}\' => Tools::getValue(\'passwd\')), $customer->email, $customer->firstname.\' \'.$customer->lastname))
						$smarty->assign(\'confirmation\', 1);
					$cookie->id_customer = intval($customer->id);
					$cookie->customer_lastname = $customer->lastname;
					$cookie->customer_firstname = $customer->firstname;';
			$new8d='array(\'{firstname}\' => $customer->firstname, \'{lastname}\' => $customer->lastname, \'{dni}\' => $customer->dni, \'{email}\' => $customer->email, \'{passwd}\' => Tools::getValue(\'passwd\')), $customer->email, $customer->firstname.\' \'.$customer->lastname))
						$smarty->assign(\'confirmation\', 1);
					$cookie->id_customer = intval($customer->id);
					$cookie->customer_lastname = $customer->lastname;
					$cookie->customer_firstname = $customer->firstname;
					$cookie->customer_dni = $customer->dni;';
			$old8e='{
			$cookie->id_customer = intval($customer->id);
			$cookie->customer_lastname = $customer->lastname;
			$cookie->customer_firstname = $customer->firstname;';
			$new8e='{
			$cookie->id_customer = intval($customer->id);
			$cookie->customer_lastname = $customer->lastname;
			$cookie->customer_firstname = $customer->firstname;
			$cookie->customer_dni = $customer->dni;';
			$openfile8 = file_get_contents($src8);
			$search8 = array($old8a,$old8b,$old8c,$old8d,$old8e);
			$replace8 = array($new8a,$new8b,$new8c,$new8d,$new8e);
			$patch8 = str_replace ($search8, $replace8, $openfile8);
			fclose ($openfile8);
			$openfile8 = file_put_contents($src8, $patch8);


			//make copy of original AdminCustomers.php
			$src9 = PS_ADMIN_DIR.'/tabs/AdminCustomers.php';
			$dest9 = dirname(__FILE__).'/bak/AdminCustomers.php';
			if ( !copy($src9, $dest9))
				die(Tools::displayError('Error: cannot copy Patch file !<br />-> Source: '.$src9.'<br /><- Dest: '.$dest9));
			//patch the original AdminCustomers.php
			//Muestra la información del DNI en la sección de clientes del panel de administración
			$old9a='l(\'First name\'), \'width\' => 60),';
			$new9a='l(\'First name\'), \'width\' => 60),
		\'dni\' => array(\'title\' => $this->l(\'TIN\'), \'width\' => 50),';
			$old9b='getFieldValue($obj, \'firstname\'), ENT_COMPAT, \'UTF-8\').\'" /> <sup>*</sup>
					<span class="hint" name="help_box">\'.$this->l(\'Forbidden characters:\').\' 0-9!<>,;?=+()@#"�{}_$%:<span class="hint-pointer">&nbsp;</span></span>
				</div>';
			$new9b='getFieldValue($obj, \'firstname\'), ENT_COMPAT, \'UTF-8\').\'" /> <sup>*</sup>
					<span class="hint" name="help_box">\'.$this->l(\'Forbidden characters:\').\' 0-9!<>,;?=+()@#"�{}_$%:<span class="hint-pointer">&nbsp;</span></span>
				</div>
				<label>\'.$this->l(\'TIN:\').\'</label>
				<div class="margin-form">
					<input type="text" size="33" name="dni" value="\'.htmlentities($this->getFieldValue($obj, \'dni\'), ENT_COMPAT, \'UTF-8\').\'" /> <sup>*</sup>
					<span class="hint" name="help_box">\'.$this->l(\'Forbidden characters:\').\' -!<>,;?=+()@#"�{}_$%:<span class="hint-pointer">&nbsp;</span></span>
				</div>';
			$old9c='margin-bottom: 5px" /><br />';
			$new9c='margin-bottom: 5px" /><br />
			\'.$this->l(\'TIN:\').\' \'.sprintf($customer->dni).\'<br />';
			$openfile9 = file_get_contents($src9);
			$search9 = array($old9a,$old9b,$old9c);
			$replace9 = array($new9a,$new9b,$new9c);
			$patch9 = str_replace ($search9, $replace9, $openfile9);
			fclose ($openfile9);
			$openfile9 = file_put_contents($src9, $patch9);
			

			//make copy of original AdminOrders.php
			$src11 = PS_ADMIN_DIR.'/tabs/AdminOrders.php';
			$dest11 = dirname(__FILE__).'/bak/AdminOrders.php';
			if ( !copy($src11, $dest11))
				die(Tools::displayError('Error: cannot copy Patch file !<br />-> Source: '.$src11.'<br /><- Dest: '.$dest11));
			//patch the original AdminOrders.php
			//Muestra el DNI en los detalles de información del cliente en la ventana de pedidos
			$old11a='.$customer->lastname.\'</a></span> (\'.$this->l(\'#\').$customer->id.\')<br />';
			$new11a='.$customer->lastname.\'</a></span> (\'.$this->l(\'#\').$customer->id.\')<br />
			\'.$this->l(\'TIN:\').\' \'.$customer->dni.\'<br />';
			$openfile11 = file_get_contents($src11);
			$search11 = array($old11a);
			$replace11 = array($new11a);
			$patch11 = str_replace ($search11, $replace11, $openfile11);
			fclose ($openfile11);
			$openfile11 = file_put_contents($src11, $patch11);
?>